package com.itheima.service.backend.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.mapper.backend.CheckGroupMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.backend.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 检查组服务实现类
 */
@Service
@Slf4j
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupMapper checkGroupMapper;

    /**
     * 新增检查组
     * @param checkGroup
     * @param checkitemIds
     */
    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        log.info("新增检查组：{}",checkGroup);
        checkGroupMapper.add(checkGroup);
        if (checkitemIds != null && checkitemIds.length > 0) {
            for (Integer checkitemId : checkitemIds) {
                checkGroupMapper.addCheckGroupAndCheckItem(checkGroup.getId(), checkitemId);
            }
        }
    }

    /**
     * 删除检查组
     * @param id
     */
    @Override
    public Result delete(Integer id) {
        log.info("删除检查组：{}",id);
        if (checkGroupMapper.findSetmealIdsByCheckGroupId(id) != null &&  checkGroupMapper.findSetmealIdsByCheckGroupId(id).size() > 0){
            return new Result(false, "该检查组被引用，不能删除");
        }else{
            checkGroupMapper.deleteCheckGroupAndCheckItem(id);
            checkGroupMapper.delete(id);
            return new Result(true, "删除检查组成功");
        }
    }

    /**
     * 根据套餐id查询检查组id集合
     * @param id
     * @return
     */
    @Override
    public List<Integer> findCheckGroupIdsBySetmealId(Integer id) {
        log.info("根据套餐id查询检查组id：{}", id);
        return checkGroupMapper.findCheckGroupIdsBySetmealId(id);
    }

    /**
     * 编辑检查组
     * @param checkGroup
     */
    @Override
    public Result edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        log.info("编辑检查组：{}", checkGroup);
        Integer id = checkGroup.getId();
        if (checkGroupMapper.findSetmealIdsByCheckGroupId(id) != null && checkGroupMapper.findSetmealIdsByCheckGroupId(id).size() > 0){
            return new Result(false, "该检查组被引用，不能编辑");
        }else{
            checkGroupMapper.deleteCheckGroupAndCheckItem(id);
            if (checkitemIds != null && checkitemIds.length > 0) {
                for (Integer checkitemId : checkitemIds) {
                    checkGroupMapper.addCheckGroupAndCheckItem(id, checkitemId);
                }
            }
            checkGroupMapper.edit(checkGroup);
            return new Result(true, "编辑检查组成功");
        }
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        log.info("分页查询：{}", queryPageBean);
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Object> page = checkGroupMapper.findPage(queryPageBean);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 查询所有检查组
     * @return
     */
    @Override
    public List<CheckGroup> findAll() {
        log.info("查询所有检查组");
        return checkGroupMapper.findAll();
    }

    /**
     * 根据检查组id查询检查组
     * @param id
     * @return
     */
    @Override
    public CheckGroup findById(Integer id) {
        log.info("根据id查询检查组：{}", id);
        return checkGroupMapper.findById(id);
    }
}
